package dao;

import logic.Pilot;
import logic.Stewardess;
import org.hibernate.Session;
import util.HibernateUtil;

import java.util.List;

public class StewardessDAOImpl {

    private Session sessionFactory;

    public void addStewardess(Stewardess stewardess) {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        sessionFactory.save(stewardess);
        sessionFactory.getTransaction().commit();
        sessionFactory.close();
    }

    public List<Stewardess> listStewardess() {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        List<Stewardess> stewardess = sessionFactory.createCriteria(Stewardess.class).list();
        sessionFactory.getTransaction().commit();
        sessionFactory.close();

        return stewardess;
    }

    public List<Stewardess> getStewardessById(int stewardessID) {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        List<Stewardess> stewardess = sessionFactory.createSQLQuery("select * from Stewardesses where id = " + stewardessID ).addEntity(Stewardess.class).list();
        sessionFactory.getTransaction().commit();
        sessionFactory.close();

        return stewardess;
    }

    public void removeStewardess(Integer id) {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        Stewardess stewardess = (Stewardess) sessionFactory.load(Stewardess.class, id);
        if (stewardess != null) {
            sessionFactory.delete(stewardess);
        }
        sessionFactory.getTransaction().commit();
        sessionFactory.close();
    }
}
